<template>
  <div class="nsqa-box">
    <!-- <div class='job-banner ct' v-if="showBanner">
    <div class="banner-text">
      <div class="c-new-title">
        农事问答
      </div>
      <div class="c-new-text">
        “站院联建”农技推广服务平台
      </div>
    </div>
    </div> -->
    <div class="s-content" v-if="showRoot">
      <div class="s-search">
        <!-- <el-cascader v-model="type1" @change="getPageQuestionAnswer(0)" clearable :props="props" placeholder='选择问题类型'/> -->

        <el-input
          v-model="input1"
          class="s-input"
        >
          <template #append>
            <div class="cp" style="font-size: 18px;" @click="search1">搜索</div>
          </template>
        </el-input>

        <div class="s-input-ti" @click="gonspost">提问</div>
      </div>
      <div class="s-detal item-content">
        <div class="new-info">
          <div class="info-item" @click="goDetail(item.forumId)" v-for="item in  works.list">
            <div class="info-i-t">
              <div class="i-l">{{item.title}}</div>
              <div class="i-r">{{ item.releaseTime.slice(0,11) }}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="item-page">
        <el-pagination
            :current-page="works.page"
            :page-size="15"
            background
            layout="prev, pager, next, jumper"
            prev-text="上一页"
            next-text="下一页"
            :total="works.total"
            @current-change="getPageQuestionAnswer"
        />
      </div>
    </div>
    <router-view></router-view>
    
    <!-- 悬浮机器人图片 -->
    <div class="floating-robot" @click="openRobotPage">
      <img src="../../assets/nsqa/robot.png" alt="机器人助手">
      <div class="robot-text">AI问答助手</div>
    </div>
  </div>
</template>
<script setup>
import { computed, reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { pageQuestionAnswer, searchForum, listFirstType, listSecondaryType } from '@/api/home'

  const router = useRouter();
  const goDetail = (id) => {
    router.push('/nsqa/nsqaDetail/'+id)
  }
  console.log('router?.currentRoute?.value',router?.currentRoute?.value)
  const showBanner = computed(() => {
    return !router?.currentRoute?.value?.meta?.hideBanner
  })
  const showRoot = computed(() => {
    return router?.currentRoute?.value?.meta?.id == 0
  })

  const search1 = () => {
    works.question = input1.value;
    getPageQuestionAnswer(0)
  }
  const input1 = ref('')

  const gonspost = () => {
    if(!localStorage.getItem('userInfo_keji')) {
			ElMessage.error('请先登录')
			return
		}
    router.push('/nsqa/nsqaPost')
  }

  const openRobotPage = () => {
    window.open('https://ai-zxn.haimijiaoyu.com/#/', '_blank')
  }

  let type1 = ref();

  const first = ref([]);
  const first1 = ref([]);

  const getlistFirstType = async () => {
    const ret = await listFirstType();
    console.log('aaa',ret)
    first.value = (ret?.data || [])?.map((item,indx) => ({
        value: item.firstTypeId,
        label: item.firstType,
        leaf: false
    }));
  }

  const getlistSecondaryType = async (firstTypeIds) => {
    const ret = await listSecondaryType({
      firstTypeIds
    });
    console.log('bbb',ret)
    first1.value = (ret?.data || [])?.filter(item => item.firstTypeId == firstTypeIds).map((item,indx) => ({
        value: item.secondaryTypeId,
        label: item.secondaryType,
        leaf: true
    }));
  };

  getlistFirstType();

  const props = computed(() => {
    return  {
    lazy: true,
    async lazyLoad(node, resolve) {
      const { level, value } = node;
      if(level == 0) {
        if(!first.value?.length) {
          await getlistFirstType();
          resolve(first.value)
        } else { 
          resolve(first.value)
        }
      } else if (level == 1) {
        await getlistSecondaryType(value);
        resolve(first1.value)
      }
      // setTimeout(() => {
      //   const nodes = Array.from({ length: level + 1 }).map((item) => ({
      //     value: ++id,
      //     label: `Option - ${id}`,
      //     leaf: level >= 2,
      //   }))
      //   // Invoke `resolve` callback to return the child nodes data and indicate the loading is finished.
      //   resolve(nodes)
      // }, 1000)
    },
  }
  })

    // 
  const works = reactive({
    list: [],
    total: 0,
    page: 1,
  });
  const getPageQuestionAnswer = async(pageNum=1) => {
    const params = {
      params: {
        // status: true,
        // question: input1.value
        // courtyardId: id.value,
        // forumType: type1.value?.[1],
        title: input1.value
      },
      page: {
        pageSize: 15,
        pageNum,
      }
    }
    const ret = await searchForum(params);
    console.log('-ret--', ret)
    works.list = ret?.data?.list || [];
    works.total = ret?.data?.total;
    works.page = pageNum;
  }
  getPageQuestionAnswer()


</script>
<style lang="scss" scoped>
.nsqa-box {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  color: #777;
  .job-banner {
    width: 100%;
    height: 280px;
    background: url(../../assets/nsqa/1.png) no-repeat center center / cover;
    padding: 0 10%;
    display: flex;
    align-items: center;
    color: #fff;
    .banner-text {
      padding-left: 30px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      position: relative;
      &::after {
        position: absolute;
        left: 0;
        top: 12px;
        height: 84%;
        width: 4px;
        background-color: #fff;
        content: ''
      }
      .c-new-title {
        font-weight: 700;
        font-size: 50px;
      }
      .c-new-text {
        font-size: 30px;
      }
    }
  }
  .s-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #f8f8f8 url('../../assets/job/6.png') no-repeat 0 200px;
    min-height: 1000px;
    width: 100%;
    padding: 0 10%;
    margin-top: 35px;
    font-size: 24px;
  }
  .s-search {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    :deep(.s-input) {
      font-size: 20px;
      height: 50px;
      width: 50%;
      .el-input__wrapper {
        background-color: #fff;
        box-shadow: none;
      }
      .el-input-group__append {
        color: #fff;
        background-color: #068043;
        height: 100%;
        padding: 0 30px;
        border-radius: 5px;
        cursor: pointer;
      }
    }
    .s-input-ti {
      color: #fff;
      background-color: #068043;
      padding: 0 30px;
      border-radius: 5px;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-left: 30px;
      cursor: pointer;
    }
  }
  .s-detal {
    margin-top: 28px;
    flex: 1;
    overflow: hidden;
    background-color: #fff;
  }
  .item-content {
    padding-top: 5px;
    display: flex;
    flex-direction: column;
    .new-info {
      flex: 1;
      background-color: #fff;
      padding: 20px 40px;
      overflow: hidden;
      color: #777;
      .info-item {
        border-bottom: 2px solid #e5e5e5;
        cursor: pointer;
        &:last-child{
          border-bottom: 0 none;
        }
      }
      .info-i-t {
        font-size: 24px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 15px 0;
        .i-l {
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
        }
        .i-r {
          margin-left: 40px;
        }
      }
    }
  }
  .item-page {
      height: 160px;
      display: flex;
      align-items: center;
      justify-content: center;
      :deep(.el-pagination) {
        button, li {
          background-color: transparent;
          width: 50px;
          height: 50px;
          border: 2px solid #e5e5e5;
          border-radius: 5px;
          color: #777;
          font-size: 24px;
          font-weight: normal;
          margin-right: 30px;
        }
        button {
          border: 0 none;
        }
        .el-pagination__jump {
          color: #777;
          font-size: 26px;
        }
        .el-pagination__editor {
          min-width: 50px;
          min-height: 50px;
          font-size: 24px;
        }
        .el-input__wrapper {
          padding: 0;
        }
        .el-input__inner {
          color: #777 !important;
        }
      }
  }

  // 悬浮机器人样式
  .floating-robot {
    position: fixed;
    bottom: 150px;
    right: 50px;
    width: 150px;
    height: 180px;
    cursor: pointer;
    z-index: 1000;
    transition: transform 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    
    &:hover {
      transform: scale(1.1);
    }
    
    img {
      width: 150px;
      height: 150px;
      object-fit: contain;
    }
    
    .robot-text {
      color: #068043;
      font-size: 14px;
      font-weight: 600;
      margin-top: 5px;
      text-align: center;
      white-space: nowrap;
      text-shadow: 1px 1px 2px rgba(255, 255, 255, 0.8);
    }
  }
}
</style>